<%+cbi/valueheader%>

<%-
require "string"
local h    = require "luci.http"
     local io   = require "nixio"
     local flag = true
     local run  = true
     local fd   = nil
     local msg_flag  = true
     local count = 0
     local msg = nil

             os.execute("rm -rf    /tmp/upload")
                 os.execute("mkdir -p   /tmp/upload")

     -- 在这之前是不能有任何语句，声明语句除外
     h.setfilehandler(
         function(field, chunk, eof)
           
             if not field or not run then return end
             
             if flag then
                 h.write(translate("Uploading ..."))
                 flag = false
             end
 
             -- 将上传的文件保存到根目录下
             local filename =  field.file

             local path = "/tmp/upload/" .. filename
             
             local len = string.len(filename)
             local file_ext = string.sub(filename,len-3,len) 
             file_ext = string.lower(file_ext)
             
             if file_ext ~= ".zip" then
                if msg_flag  then
                    h.write("<br />" .. translate("This is not a zip file!"))
                    msg_flag = false
                end
                return
             end

             
             local file_main = string.sub(filename,1,len-4)

             if not fd then
                 fd = io.open(path, "w")
             end
             count =  count + #chunk
             if count > 1048576 then
                msg = "file is so big!"
                else
                    fd:write(chunk)
             end
             
             
             if eof and fd then
                 fd:close()
                 fd = nil
                 --  
                 if msg ~= nil then
                    h.write("<br />" .. translate(msg))
                    os.execute("rm -rf /tmp/upload/*")
                 else
                    h.write("<br />" .. translate("upload complite!"  ))
                 
                    os.execute("rm -rf    /tmp/portal")
                    os.execute("mkdir -p   /tmp/portal")

                    os.execute("unzip  " .. path .. " -d /tmp/portal")
                    os.execute("rm -rf /usr/local/natshell/nac/upload_portal/html/*")
                    os.execute("/bin/cp -rf  /tmp/portal/*   /usr/local/natshell/nac/upload_portal/html/.")
                 
                
                 os.execute("rm -rf /tmp/portal/*")
                 os.execute("rm -rf /tmp/upload/*")

                -- os.execute("mkdir -p /tmp/ooxx/ooxx")
                end

             end
         end
     )
 
     -- 这块代码一定也要放在setfilehandler下面。
     if h.formvalue("act") == "upload" then
         return
     end

 -%>
 <form id="upload" name="upload" action="<%=REQUEST_URI%>" method="post" enctype="multipart/form-data"  onsubmit="">
     <input type="hidden" name="act" value="upload" />

         <input   type="file" id="updatePackage" name="updatePackage" />
      <input type="submit" id="updateBtn" class="cbi-button cbi-button-apply" value="<%:Upload%>" />    
    
 </form>
</br>
</br>


  <form class="inline" method="post" action="<%=url('admin/ac_nac/download')%>" onsubmit="">
                                <input type="hidden" name="token" value="<%=token%>" />
                                <div>
                                        <label class="cbi-value-title" for="image"><%:Download backup%>:</label>
                                        <div class="cbi-value-field">
                                            
                                                <button class="btn btn-success" type="submit" name="backup" >
                                                        <i class="icon icon-download-alt"></i>
                                                        <%:Download template%>
                                                </button>

                                            
                                        </div>
                                </div>
 </form>


